<?php
/**
 *邀请记录
 */

namespace app\api\model;


use app\common\model\Model;

class InviteRecord extends  Model
{
    const TABLE_NAME = 'invite_record';
    const CACHE_EXPIRE_LEVEL = self::CACHE_EXPIRE_LEVEL_1;

    const MAX_INVITE_CHAN_LEN = 3;
    protected $name = self::TABLE_NAME;

    public static function getInfo($user_id)
    {
        return self::commonQuery(self::TABLE_NAME)
            ->where(['user_id'=>$user_id])
            ->find();
    }

    /** 添加 邀请记录，生成 邀请记录链， 返回 当前注册用户 注册成功后添加积分 的邀请人数组
     * @param $login_uid
     * @param $invite_user_id
     * @return array
     */
    public static function addRecord($login_uid,$invite_user_id)
    {

        $info = self::getInfo($invite_user_id);

        $arr_invite_chan = [$invite_user_id];

        if(empty($info) || $info['invite_chan_len']>=self::MAX_INVITE_CHAN_LEN) {
            $invite_chan_len = 1;
            $invite_chan = ',' . $invite_user_id . ',';
            if (empty($info)) {
                $master_id = $invite_user_id;
            } else {
                $master_id = $info['master_id'];

                $arr_invite_chan = array_merge($arr_invite_chan, explode(',', substr($info['invite_chan'], 1, -1)));
            }
        }else{
            $invite_chan_len =$info['invite_chan_len']+1;
            $invite_chan =','.$invite_user_id.$info['invite_chan'];
            $master_id = $info['master_id'];
            $cur_invite_chan_arr = explode(',',substr($info['invite_chan'],1,-1));

            // 当为 截断 的链时， 需 返回  上个链数据 中 的 第一个数据
            //这样 构成 最近 的 3 个邀请人数组
            if($info['invite_chan_len'] == 1){
                $arr_invite_chan[] = $cur_invite_chan_arr[0];
                $last_invite_chan_info = self::getInfo($cur_invite_chan_arr[0]);
                if(!empty($last_invite_chan_info))
                {
                   $last_invite_chan_arr = explode(',',substr($last_invite_chan_info['invite_chan'],1,-1));
                   $arr_invite_chan[] = $last_invite_chan_arr[0];
                }
            }else{
                $arr_invite_chan =  array_merge($arr_invite_chan,$cur_invite_chan_arr);
            }
        }

        self::commonQuery(self::TABLE_NAME)
               ->insert([
                   'user_id'=>$login_uid,
                   'master_id'=>$master_id,
                   'invite_chan'=>$invite_chan,
                   'invite_chan_len'=>$invite_chan_len
               ]);

        return ['master_id'=>$master_id,'invite_chan'=>$arr_invite_chan];
    }

    //获取 用户的邀请记录
    public static function getUserInviteRecord($user_id,$page=1,$limit=self::LIMIT)
    {
        return self::commonQuery(self::TABLE_NAME)
               ->where(['invite_chan'=>['like',','.$user_id.'%']])
               ->page($page,$limit)
              ->order('user_id desc')
              ->select();
    }
    public static function handleInviteRecordList($list)
    {
        $res =[];
        if(empty($list))
        {
            return [];
        }

        foreach ($list as $key=>$val)
        {
            $res[$key]=User::handleUserCommonInfo($val['user_id']);
            $points_record_info = PointsRecord::getRecordInfo($val['user_id'],PointsRecord::POINTS_INVITE_LEVEL_1);
            $res[$key]['record_addtime']=date('Y-m-d H:i',$points_record_info['record_addtime']);
            $res[$key]['record_points']='+'.$points_record_info['record_points'];
        }

        return $res;
    }
}